//
//  TCHomeViewController.m
//  TongCheDemo
//
//  Created by cwMac on 16/1/22.
//  Copyright © 2016年 cwMac. All rights reserved.
//

#import "TCHomeViewController.h"
#import "ZDMenuView.h"
#import "BMBannerView.h"
#import "TCPersonViewController.h"
#import "ZDAccountSecureController.h"
#import "BMWebController.h"
#import "BMAD.h"
#import "BMAccountManager.h"
#import "BMLoginController.h"
#import "ZDCreateOrderController.h"
@interface TCHomeViewController ()
@property(nonatomic,strong)ZDMenuView *menuView;
@property(nonatomic,weak)IBOutlet BMBannerView *bannerView;
@property(nonatomic,weak)IBOutlet NSLayoutConstraint *menuHeightConstraint;
-(IBAction)showMenu;
-(IBAction)searchCarAction:(UITapGestureRecognizer *)recognizer;
@end

@implementation TCHomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.edgesForExtendedLayout = UIRectEdgeNone;
    
    if (kScreenHeight >= 736) {
        _menuHeightConstraint.constant = 110.f;
    } else if (kScreenHeight == 568) {
        _menuHeightConstraint.constant = 85.f;
    } else if (kScreenHeight == 480) {
        _menuHeightConstraint.constant = 82.f;
    } else {
        _menuHeightConstraint.constant = 100.f;
    }
    BMLog(@"%.2f, %.2f", kScreenWidth, kScreenHeight);
    
    weakSelf(self);
    self.menuView = [[ZDMenuView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    NSURL *url = [[NSBundle mainBundle] URLForResource:@"ZDmenu" withExtension:@"plist"];
    self.menuView.menuItems = [NSArray arrayWithContentsOfURL:url];
    [self.menuView setMenuClicked:^(NSIndexPath *indexPath) {
        
        if (!indexPath) {
            TCPersonViewController *personVC = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"persnoVC"];
            [weakSelf showViewController:personVC sender:nil];
            return;
        }
        
        BOOL row = indexPath.row;
        UIStoryboard *storyBoaard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        UIViewController *destVC;
        switch (indexPath.section) {
            case 0:
                if (row) {
                    destVC = [storyBoaard instantiateViewControllerWithIdentifier:@"orderVC"];
                }
                break;
            case 1:
                if (row) {
                    destVC = [storyBoaard instantiateViewControllerWithIdentifier:@"walletVC"];
                } else {
                    destVC = [storyBoaard instantiateViewControllerWithIdentifier:@"zdVC"];
                }
                break;
            case 2:
                if (row) {
                    destVC = [storyBoaard instantiateViewControllerWithIdentifier:@"msgVC"];
                } else {
                    destVC = [ZDAccountSecureController new];
                }
                break;
            case 3:
                destVC = [storyBoaard instantiateViewControllerWithIdentifier:@"settingVC"];
                break;
        }
        if (destVC) {
            [weakSelf showViewController:destVC sender:nil];
        }
    }];
    
    [self.bannerView setItmeClick:^(BMAD *ad) {
        BMWebController *webVC = [BMWebController new];
        webVC.path = [NSURL URLWithString:ad.linkSrc];
        [self showViewController:webVC sender:nil];
    }];
    
    [self getData];
    // Do any additional setup after loading the view.
}


- (void)getData
{
    BMRequest *request = [BMRequest requestWithPath:@"findAdList.htm" contentKey:@"adverList"];
    request.params = @{@"position": @2};
    [BMAD postWithRequest:request finish:^(BMResponse *response, NSError *error) {
        if (response.status == kResultOK) {
            self.bannerView.banners = response.result;
            [self.bannerView start];
        } else {
            [self showHint:@"获取广告列表失败"];
        }
        [self getMenuAD];
    }];
}

- (void)getMenuAD
{
    BMRequest *request = [BMRequest requestWithPath:@"findAdList.htm" contentKey:@"adverList"];
    request.params = @{@"position": @0};
    [BMAD postWithRequest:request finish:^(BMResponse *response, NSError *error) {
        if (response.status == kResultOK) {
            self.menuView.bannerView.banners = response.result;
            [self.bannerView start];
        }
    }];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self.bannerView start];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self.bannerView stop];
}

- (void)showMenu
{
    if ([[BMAccountManager sharedInstance] isLogin]) {
        [_menuView show];
    } else {
        BMLoginController *loginVC = [self.storyboard instantiateViewControllerWithIdentifier:@"loginVC"];
        [self presentViewController:loginVC animated:YES completion:nil];
//        BMLoginController *loginVC = [[BMLoginController alloc]init];
//        [self presentViewController:loginVC animated:YES completion:nil];
    }
}
//
- (IBAction)searchCarAction:(UITapGestureRecognizer *)recognizer {
    ZDCreateOrderController *createVC = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"createOrderVC"];
    createVC.defaultIndex = recognizer.view.tag;
    [self presentViewController:createVC animated:YES completion:nil];
    NSLog(@"111");
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
